# TLSH is provided for use under two licenses: Apache OR BSD.
# Users may opt to use either license depending on the license
# restictions of the systems with which they plan to integrate
# the TLSH code.
#
# ==============
# Apache License
# ==============
# Copyright 2013 Trend Micro Incorporated
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# ===========
# BSD License
# ===========
# Copyright (c) 2013, Trend Micro Incorporated
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification,
# are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this
#    list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice,
#    this list of conditions and the following disclaimer in the documentation
#    and/or other materials provided with the distribution.

# 3. Neither the name of the copyright holder nor the names of its contributors
#    may be used to endorse or promote products derived from this software without
#    specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
# IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
# INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
# OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
# OF THE POSSIBILITY OF SUCH DAMAGE.

cmake_minimum_required(VERSION 2.6)

project(TLSH)

set(VERSION_MAJOR 4)
set(VERSION_MINOR 3)
set(VERSION_PATCH 0)

# TLSH uses only half the counting buckets.
# It can use all the buckets now.
set(TLSH_BUCKETS_128 1)
if(TLSH_BUCKETS_48 EQUAL 1)
  set(TLSH_HASH "min hash")
  add_definitions(-DBUCKETS_48)
endif()
if(TLSH_BUCKETS_128 EQUAL 1)
  set(TLSH_HASH "compact hash")
  add_definitions(-DBUCKETS_128)
endif()
if(TLSH_BUCKETS_256 EQUAL 1)
  set(TLSH_HASH "full hash")
  add_definitions(-DBUCKETS_256)
endif()

# TLSH uses 1 byte checksum. The collision rate is 1 in 24.
# It can use 3 bytes checksum now. That collision rate in 1 in 5800.
set(TLSH_CHECKSUM_1B 1)

if(TLSH_CHECKSUM_0B EQUAL 1)
  set(TLSH_CHECKSUM "no checksum")
  add_definitions(-DCHECKSUM_0B)
endif()
if(TLSH_CHECKSUM_1B EQUAL 1)
  set(TLSH_CHECKSUM "1 byte checksum")
endif()
if(TLSH_CHECKSUM_3B EQUAL 1)
  set(TLSH_CHECKSUM "3 bytes checksum")
  add_definitions(-DCHECKSUM_3B)
endif()

# setting TLSH_DISTANCE_PARAMETERS to 1 allows you to set command line arguments
# to set - and hence experiment with the distance parameters
#	by default this is zero
set(TLSH_DISTANCE_PARAMETERS 0)

# write a file with the VERSION information
file(REMOVE VERSION)
file(WRITE VERSION 
  "// This file is generated by cmake.  Modify\n"
  "// CMakeLists.txt to change the VERSION numbers\n"
  "TLSH version: ${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH} ${TLSH_HASH}, ${TLSH_CHECKSUM}\n")
  
file(REMOVE include/version.h)
file(WRITE include/version.h
  "/****************************************************\n"
  " * This file is generated by cmake.  Modify the top\n"
  " * level CMakeLists.txt to change the VERSION numbers\n"
  " ****************************************************/\n\n"
  "#define VERSION_MAJOR ${VERSION_MAJOR}\n"
  "#define VERSION_MINOR ${VERSION_MINOR}\n"
  "#define VERSION_PATCH ${VERSION_PATCH}\n"
  "#define TLSH_HASH \"${TLSH_HASH}\"\n"
  "#define TLSH_CHECKSUM \"${TLSH_CHECKSUM}\"\n")
if(TLSH_DISTANCE_PARAMETERS EQUAL 1)
file(APPEND include/version.h
  "#define TLSH_DISTANCE_PARAMETERS ${TLSH_DISTANCE_PARAMETERS}\n")
endif()

if (CMAKE_BUILD_TYPE STREQUAL Debug)
    if(CMAKE_COMPILER_IS_GNUCXX)
        set(CMAKE_CXX_FLAGS "-g")
    endif()
else(CMAKE_BUILD_TYPE STREQUAL Debug)
    if(CMAKE_COMPILER_IS_GNUCXX)
        set(CMAKE_CXX_FLAGS "-O2 -fvisibility=internal")  ## Remove TlshImpl symbols
    endif()
    if(MSVC)
        set(CMAKE_CXX_FLAGS "/O2")        ## Optimize
    endif()
endif(CMAKE_BUILD_TYPE STREQUAL Debug)

if(MSVC)
    add_definitions(-DWINDOWS -DTLSH_LIB)
    include_directories(Windows)
endif()

# user can override CXX; make sure tests link and load properly regardless of LD_LIBRARY_PATH
if(CMAKE_COMPILER_IS_GNUCXX)
    set(CMAKE_EXE_LINKER_FLAGS "-static-libgcc")
    set(CMAKE_EXE_LINKER_FLAGS "-static-libstdc++")
endif()

enable_testing()

include_directories(include)
add_subdirectory(src)
add_subdirectory(test)
add_subdirectory(utils)
add_subdirectory(Testing)

if(MSVC)
    add_subdirectory(Windows)
endif()

set(CPACK_GENERATOR "TGZ")
set(CPACK_SOURCE_GENERATOR "TGZ")
set(CPACK_SOURCE_PACKAGE_FILE_NAME "TLSH-${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH}")
set(CPACK_PACKAGE_VERSION_MAJOR "${VERSION_MAJOR}")
set(CPACK_PACKAGE_VERSION_MINOR "${VERSION_MINOR}")
set(CPACK_PACKAGE_VERSION_PATCH "${VERSION_PATCH}")
set(CPACK_SOURCE_IGNORE_FILES
"~$"
"^${PROJECT_SOURCE_DIR}/bin/"
"^${PROJECT_SOURCE_DIR}/lib/"
"^${PROJECT_SOURCE_DIR}/build/"
"^${PROJECT_SOURCE_DIR}/py_ext/data/"
"^${PROJECT_SOURCE_DIR}/py_ext/build/"
"^${PROJECT_SOURCE_DIR}/Testing/tmp/"
"^${PROJECT_SOURCE_DIR}/CMakeFiles/"
)
include(CPack)

